package org.example.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.example.config.ThreadConfig;
import org.example.entity.SysLogs;
import org.example.service.ISysLogsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletMapping;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Enumeration;

@Aspect
@Component
public class LogRecordAop {
    @Autowired
    HttpServletRequest request;
    @Autowired
    ISysLogsService iSysLogsService;
    @Autowired
    private ThreadConfig asyncServiceExecutor;
    @Autowired
    ThreadPoolTaskExecutor threadPoolTaskExecutor;
    @Pointcut("@annotation(org.example.aop.Log)")
    public void logg(){

    }

    @Around("logg()")
    public Object aop(ProceedingJoinPoint joinPoint) throws Throwable {

            System.err.println("111111=======");
            String servletPath = request.getServletPath();
            String methodName = servletPath.substring(servletPath.lastIndexOf("/"), servletPath.length());
            String method = request.getMethod();
            String remoteUser = request.getRemoteHost();
            System.err.println("ip======"+remoteUser);
            System.err.println("方法名称："+methodName);
            System.err.println("访问路径："+servletPath);
            System.err.println("访问的方法类型=="+method);
            try {
                System.err.println("ee===="+joinPoint.proceed());
            } catch  (Throwable e) {
                throw new RuntimeException(e);
            }
            SysLogs sysLogs = new SysLogs();
            sysLogs.setIp(remoteUser);
            sysLogs.setAccessType(method);
            sysLogs.setAccessName(methodName);
            sysLogs.setCreateTime(new Date());
            sysLogs.setAccessUrl(servletPath);
            threadPoolTaskExecutor.submit(()->{
                iSysLogsService.save(sysLogs);
            });
            threadPoolTaskExecutor.shutdown();
        return joinPoint.proceed();
    }
}
